﻿using EllenTang.IDal;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.Entity;

namespace EllenTang.DalFactory
{
    public class DBSession:IDBSession
    {
        IAccessStatistics _AccessStatistics;

        /// <summary>
        /// 调用GetAccessAccessStatisticsDal获取实例
        /// </summary>
        public IAccessStatistics AccessStatistics
        {
            get
            {
                if (_AccessStatistics == null)
                {
                    _AccessStatistics = AbstractFactory.GetAccessAccessStatisticsDal();
                }
                return _AccessStatistics;
            }
            set
            {
                _AccessStatistics = value;
            }
        }

        IAccessTable _AccessTable;

        /// <summary>
        /// 调用GetAccessTableDal获取实例
        /// </summary>
        public IAccessTable AccessTable
        {
            get
            {
                if (_AccessTable == null)
                {
                    _AccessTable = AbstractFactory.GetAccessTableDal();
                }
                return _AccessTable;
            }
            set
            {
                _AccessTable = value;
            }
        }

        public DbContext Db { get { return Dal.DbContextFactory.dbContext; } }

        /// <summary>
        /// 将对数据库的操作汇总在此,统一进行操作
        /// </summary>
        /// <returns>结果</returns>
        public bool SaveChange()
        {
            return Db.SaveChanges() > 0;
        }
    }
}
